
/**
 * Author: [author]
 * Date: [date]
 * Description: 
 */

class [name]View @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr), LifecycleOwner, ViewModelStoreOwner {
    
    private val mAppViewModelStore: ViewModelStore = ViewModelStore()
    private val mRegistry = LifecycleRegistry(this)
    
    
    /**
     * ================= ViewModelStoreOwner ================================
     */
    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        mRegistry.currentState = Lifecycle.State.CREATED
        LogUtils.d(TAG, "view created")
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        mViewModelStore.clear()
        mRegistry.currentState = Lifecycle.State.DESTROYED
        LogUtils.d(TAG, "view destroyed")
    }

    override fun onWindowVisibilityChanged(visibility: Int) {
        super.onWindowVisibilityChanged(visibility)
        if (visibility == VISIBLE) {
            LogUtils.d(TAG, "view show")
            mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
            mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
        } else if (visibility == GONE || visibility == INVISIBLE) {
            LogUtils.d(TAG, "view hide")
            mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE)
            mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
        }
    }
    
    override fun getViewModelStore(): ViewModelStore {
        return mAppViewModelStore
    }

    override fun getLifecycle(): Lifecycle {
        return mRegistry
    }
}
